package com.activenetwork.smlite.testcase.events.eventListSection;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import com.activenetwork.custom.uielement.decorate.IdentifierFactory;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.grid.OperatableGrid;
import com.activenetwork.util.grid.SortableGrid;
import com.activenetwork.util.grid.SortableGrid.ColumnSort;
import com.activenetwork.util.grid.UnsortableException;
import com.activenetwork.util.grid.condition.ConditionBuilder;
import com.activenetwork.util.grid.gson.GridFactory;
import com.activenetwork.util.verification.gson.FailurePoint;

public class EventListSection001 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(EventListSection001.class);
	
	@Test
	public void UI_Verify_Event_List_data_grid_displays_correctly(){
		loginBusiness.login();
		
		// Verify 1st column is checkbox
		List<WebElement> checkboxes = webDriver.findElements(By.cssSelector(".main-content.autotest-bamboo-eventshomeview table tbody[style=''] tr td:first-child input"));
		for(WebElement element : checkboxes){
			String type = element.getAttribute("type").trim();
			if(!StringUtils.equalsIgnoreCase(type, "checkbox")){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(EventListSection001.class.getName(), 
						"Verify event grid 1st column is checkbox", 
						"Event grid 1st column is not checkbox"));
				break;
			}
		}
		
		// Verify the table header content
		List<WebElement> ths = webDriver.findElements(By.cssSelector(".main-content.autotest-bamboo-eventshomeview table thead th.nexus-table-header"));
		if(ths.size() < 6){
			for(WebElement element : ths){
				logger.error("Event grid header: " + element.getText().trim());
			}
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection001.class.getName(), 
					"Verify event grid header size", 
					"Event grid header size is less than 6"));
		}else{
			String eventTh = ths.get(1).getText().trim();
			String startDateTh = ths.get(2).getText().trim();
			String registrantsTh = ths.get(3).getText().trim();
			String locationTh = ths.get(4).getText().trim();
			String tagTh = ths.get(5).getText().trim();
			
			if(!StringUtils.equals(eventTh, "EVENT")){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(EventListSection001.class.getName(), 
						"Verify event grid EVENT header", 
						"Event grid event header is not 'EVENT'"));
			}
			if(!StringUtils.equals(startDateTh, "START DATE")){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(EventListSection001.class.getName(), 
						"Verify event grid start date header", 
						"Event grid start date header is not 'START DATE'"));
			}
			if(!StringUtils.equals(registrantsTh, "REGISTRANTS")){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(EventListSection001.class.getName(), 
						"Verify event grid registrants header", 
						"Event grid registrants header is not 'REGISTRANTS'"));
			}
			if(!StringUtils.equals(locationTh, "LOCATION")){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(EventListSection001.class.getName(), 
						"Verify event grid location header", 
						"Event grid location header is not 'LOCATION'"));
			}
			if(!StringUtils.equals(tagTh, "TAGS")){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(EventListSection001.class.getName(), 
						"Verify event grid tags header", 
						"Event grid tags header is not 'TAGS'"));
			}
		}
		
		// Verify the table header sortable
		
		// the grid on the home view page
		SortableGrid grid = GridFactory.getGrid("EventsHomeView", "SearchEventGrid");
		
		List<String> unsortableColumn = new ArrayList<String>();
		for(int i=2;i<=ths.size();i++){
			try{
				grid.sort(i, ColumnSort.ASC);
			}catch(UnsortableException e){
				unsortableColumn.add(String.valueOf(i));
			}
		}
		
		if(unsortableColumn.contains("2")){ //EVENT column should be sortable
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection001.class.getName(), 
					"Verify event grid event column sortable", 
					"Event column is not sortable"));
		}
		if(unsortableColumn.contains("3")){ // START DATE column should be sortable
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection001.class.getName(), 
					"Verify event grid start date column sortable", 
					"Start date column is not sortable"));
		}
		
		
		
		// Verify Event Name should be displayed as a link and clickable
		By eventNameBy = By.cssSelector(".main-content.autotest-bamboo-eventshomeview table tbody[style=''] tr td:nth-child(2)");
		List<WebElement> tds = webDriver.findElements(eventNameBy);
		for(WebElement td : tds){
			try{
				td.findElement(By.xpath(".//a"));
			}catch(NoSuchElementException e){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(EventListSection001.class.getName(), 
						"Verify event name is link", 
						"Event " + td.getText() + " name is not a link"));
			}
		}
		
		// Verify the page size is 25 if enough event records displayed on the page
		if(((OperatableGrid)grid).isPagable()){
			ConditionBuilder builder = new ConditionBuilder();
			int pageSize = ((OperatableGrid)grid).countMatchRow(builder.build());
			if(pageSize != 25){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(EventListSection001.class.getName(), 
						"Verify event grid page size", 
						"Page size is not 25"));
			}
		}
		
		// Verify Delete and Copy icons are in the left top corner of the data grid.
		try{
			IdentifierFactory.createUIElement("EventsHomeView", "deleteEvent");
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection001.class.getName(), 
					"Verify delete event button", 
					"Unable to find the delete event button on the page"));
		}
		
		try{
			IdentifierFactory.createUIElement("EventsHomeView", "copyEvent");
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection001.class.getName(), 
					"Verify copy event button", 
					"Unable to find the copy event button on the page"));
		}
		
		// Verify Add event button
		try{
			IdentifierFactory.createUIElement("EventsHomeView", "addEvent");
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection001.class.getName(), 
					"Verify add event button", 
					"Unable to find the add event button on the page"));
		}
		
		loginBusiness.logout();
	}
}
